package com.atguigu04.compare;

import java.util.Arrays;
import java.util.Comparator;

public class ComparatorTest {
    public static void main(String[] args) {
        Product[] products=new Product[5];
        products[0]=new Product("a",45);
        products[1]=new Product("b",213);
        products[2]=new Product("c",654);
        products[3]=new Product("d",213);
        products[4]=new Product("e",5643);

        //创建一个实现了Comparator接口的实现类的对象
        Comparator comparator=new Comparator() {
            //如何判断两个对象o1 o2的大小，标准就是此方法的方法体要编写的逻辑
            @Override
            public int compare(Object o1, Object o2) {
                if(o1==o2){
                    return 0;
                }
                if(o1 instanceof Product&&o2 instanceof Product){
                    Product p1=(Product) o1;
                    Product p2=(Product) o2;
                    return Double.compare(p1.getPrice(),p2.getPrice());
                    /*
                    * return p1.getname().compareTo(p2.getname())
                    * */
                }
                throw new RuntimeException("类型不对");
            }
        };

        Arrays.sort(products,comparator);

    }
}
